let vue = new Vue({
    el: "#employee",
    data(){
        return{
            //基本分页信息
            page:1,
            pageSize:10,
            pageCount: 20,
            totalData: 999,
            startRow:5,
            switchStatus:false,
            searchContent:'',
            flag : true,
            employees:[],
            //查询为空提示信息区域显示
            isTip:false,
            //查询选项名称
            //查询为空提示信息
            tip:'',
            //职位信息
            positionName:'职位',
            //部门信息
            departmentId:'部门',
            departments:[],
            status : 1,
            //新增表单项
            addForm:{
                addName : '',
                addLoginName:'',
                addGender:1,
                addPassword:'',
                addRePassword:'',
                addPhone:'',
                addDepartmentId:'部门',
                addPosition:'职位',
                addStatus:1,
            },
            //修改表单项
            updForm:{
                updEmId : '',
                updName : '',
                updLoginName:'',
                updDepartmentName:'',
                updGender:1,
                updPassword:'',
                updRePassword:'',
                updPhone:'',
                updPositionId:'',
                updStatus:'',
            },
            deleteMsg:{
                deleteEmId:'',
                deleteName:'',
                deleteGender:'',
                deletePhone:'',
                deleteDepartmentName:'',
                deletePositionName:''
            }
        }
    },

    methods:{
        reset(){
            this.searchContent = '';
            this.departmentId = '部门';
            this.positionName = '职位';
            this.switchStatus = false;
            this.getList(1);
        },
        //获取部门信息
        getDepartment(){
            axios.get('/dep/getDepartment').then(res=>{
                this.departments = res.data.msg;
            })
        },

        //打开修改模态框并填充信息
        toUpd(employee){
            this.updForm.updEmId = employee.emId;
            this.updForm.updPositionName = employee.position.positionName;
            this.updForm.updDepartmentName = employee.department.name;
            this.updForm.updGender = employee.gender;
            this.updForm.updPhone = employee.phone;
            this.updForm.updName = employee.name;
            this.updForm.updLoginName = employee.loginName;
            this.updForm.updStatus = employee.status;
            this.updForm.updPhone = employee.phone;
            this.updForm.updPositionId = employee.position.positionId;
        },

        toDelete(employee){
            console.log(employee);
            this.deleteMsg.deleteEmId = employee.emId;
            this.deleteMsg.deleteName = employee.name;
            this.deleteMsg.deleteGender = employee.gender;
            this.deleteMsg.deletePhone =  employee.phone;
            this.deleteMsg.deleteDepartmentName = employee.department.name;
            this.deleteMsg.deletePositionName = employee.position.positionName;
        },

        deleteMessage(id){
            axios.get('/employee/delete',{
                params:{
                    emId : id
                }
            }).then(res=>{
                if (res.data.status){
                    if (this.totalData%this.pageSize==1){
                        this.pageCount--;
                    }
                    this.getList(this.pageCount);
                    lightyear.notify(res.data.msg,"success","500","mdi mdi-checkbox-marked-circle")
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close")
                }
            })
        },

        //切换筛选状态
        switchChange(){
            this.flag = true;
            this.switchStatus = ! this.switchStatus;
            this.page = 1;
            this.getList(this.page);
        },

        //修改员工状态
        changeStatus(id,status){
            if (status==0){
                status =1;
            }else {
                status = 0;
            }
            axios.post('/employee/updStatus',{
                emId : id,
                status :status,
            }).then(res=>{
                //修改成功
                if (res.data.status){
                    //筛选状态为true
                    this.flag = true;
                    this.employees = [];
                    this.getList(this.page);
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close")
                }
            })
        },

        //添加员工信息
        addEmployee(){
            console.log(this.addForm);
            let name = this.addForm.addName;
            let loginName = this.addForm.addLoginName;
            let password = this.addForm.addPassword;
            let departmentId = this.addForm.addDepartmentId;
            let gender = this.addForm.addGender;
            let phone =this.addForm.addPhone;
            let positionName = this.addForm.addPosition;
            let status = this.addForm.addStatus;
            this.flag = true;
            axios.post('/employee/add',{
                name : name,
                loginName : loginName,
                gender : gender,
                password : password,
                depId: departmentId,
                phone : phone,
                positionName : positionName,
                status : status
            }).then(res=>{
                //修改信息成功
                if(res.data.status){
                    //获取页尾吗
                    this.getList(Math.ceil(this.totalData/this.pageSize));
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close")
                }
            })

        },

        //修改员工信息
        updEmployee(){
            console.log(this.updForm)
            axios.post('/employee/update',{
                emId : this.updForm.updEmId,
                positionId : this.updForm.updPositionId,
                positionName :this.updForm.updPositionName,
                departmentName :this.updForm.updDepartmentName,
                gender : this.updForm.updGender,
                name : this.updForm.updName,
                phone : this.updForm.updPhone,
                loginName : this.updForm.updLoginName,
                status : this.updForm.updStatus,
                page: this.page,
                pageSize: this.pageSize,
            }).then(res=>{
                if(res.data.msg){
                    this.getList(this.page);
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close")
                }
            })
        },

        log(){
            alert("部门")
        },
        //搜索栏查询
        search(){
            this.flag = true;
            this.getList(1);
        },

        //刷新员工信息
        getList(page){
            let status;
            let departmentId = this.departmentId;
            let positionName = this.positionName;
            console.log("departmentId:"+departmentId+"-============positionName"+positionName)
            if (this.switchStatus){
                status = 1;
            }else {
                status = 0;
            }
            if (positionName=='职位'){
                positionName = '';
            }
            if (departmentId =='部门'){
                departmentId = 0;
            }
            axios.get("/employee/getList",{
                params:{
                    page : this.page,
                    pageSize : this.pageSize,
                    name : this.searchContent,
                    depId : departmentId,
                    positionName : positionName,
                    status : status
                }
            }).then(res=>{
                this.employees = res.data.msg.list;
                this.flashPage(res.data.msg);
                if (this.flag){
                    this.paginationFlash();
                    this.flag = false;
                }
                if (res.data.msg.list==''||res.data.msg.list==null){
                    this.tip = '很抱歉，未找到您需要的信息~'
                }else {
                    this.tip = ''
                }
            })
        },

        //刷新分页信息
        paginationFlash(){
            $('#pagination').pagination({
                current: vue.$data.page,
                pageCount: this.pageCount,//总页码
                coping: true,
                isHide: true,
                callback: function (e){
                    vue.$data.page = e.getCurrent();
                }
            });
        },

        //分页信息刷新
        flashPage(data){
            this.tip='';
            this.pageCount = data.pages;
            this.pageSize = data.pageSize;
            this.totalData = data.total;
            this.startRow = data.startRow;
        }
    },
    created(){
        // this.getList(this.page);
        this.getDepartment();
        this.getList(this.page);
    },
    watch: {
        page: function (val) {
            this.getList(val);
        },
    }
});
